#!/usr/bin/python2.7
# coding=utf-8
# -*- coding:UTF-8 -*-

import os  # 文件操作
import commands  # 执行命令行
import shutil  # 高级的 文件、文件夹、压缩包 处理模块
import sys

import tool
import ipaReSign

# plist文件管理者
PlistBuddy = '/usr/libexec/PlistBuddy'

# plist文件开头信息
plistPrefix = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict/></plist>'

# 工作路径,获取当前脚本路径
WDir = os.path.split(sys.argv[0])[0]

# packConfig.plist 打包信息缓存
packConfig = 'packConfig.plist'
# ipa包列表缓存
ipaList = 'ipaList'
# 渠道SDK列表缓存
channelList = 'channelList'

# Payload文件夹的app名字
appName = ''

# ipa包的Info.plist文件路径
ipaPlist = ''

# ipa包路径
ipaPath = '0_ipa'

# 渠道SDK路径
channelPath = '0_Channel'

# 图片资源路径
assets = 'Assets'

# 签名包的新名字
newName = ''


# 指定工作空间
def setWorkingDir():
    '指定工作目录,rstrip()删除字符串末尾的空格'
    global WDir
    input = tool.change_symbol(raw_input('输入工作目录:').rstrip(), '-')
    if len(input):
        if os.path.isdir(input):
            os.chdir(input)
            WDir = '.'
            print '工作目录:' + os.getcwd()
        else:
            print '***Error*** : 工作目录错误'
            exit()


# 创建一个缓存打包信息的plist文件
def create_packConfig():
    '创建一个缓存打包信息的plist文件'
    # with open(os.path.join(WDir, packConfig), 'w') as packConfigFile:
    #     packConfigFile.write(plistPrefix)
    #     packConfigFile.close()
    (status, result) = commands.getstatusoutput('%s -c Set: %s' % (PlistBuddy, os.path.join(WDir, packConfig)))
    if status:
        print '执行报错:%s' % result


# 显示ipa文件列表
def show_ipaList():
    '显示ipa文件列表'
    # 判断文件夹是否存在
    if os.path.exists(os.path.join(WDir, ipaPath)):
        # print "[ 0_ipa ]存在\n"
        # 执行命令行
        # 删除旧缓存 ipaList
        (status, result) = commands.getstatusoutput('%s -c "Delete :%s" %s' % (PlistBuddy, ipaList, os.path.join(WDir, packConfig)))
        if status:
            # print '执行报错:%s' % result
            pass

        # 新建新缓存 ipaList
        (status, result) = commands.getstatusoutput('%s -c "Add :%s array" %s' % (PlistBuddy, ipaList, os.path.join(WDir, packConfig)))
        if status:
            print '执行报错:%s' % result

        # 遍历0_ipa文件夹中的文件列表
        print ' ipa包列表 '.center(80, '*')
        fileList = os.listdir(os.path.join(WDir, ipaPath))
        count = 0
        for i in range(0, len(fileList)):
            if fileList[i].endswith('.ipa'):
                path = os.path.join(os.path.join(WDir, ipaPath), fileList[i])
                if os.path.isfile(path):
                    ipaName = fileList[i].replace('.ipa', '')
                    print '[%d] - %s' % (count + 1, ipaName)
                    (status, result) = commands.getstatusoutput(
                        '%s -c "Add :%s:%d string %s" %s' % (PlistBuddy, ipaList, count, ipaName, os.path.join(WDir, packConfig)))
                    if status:
                        print '执行报错:%s' % result
                    else:
                        count += 1
            else:
                pass
                # print '[-] - (无效文件)%s' % fileList[i]

        print ' ipa包列表 '.center(80, '*')
        if count == 0:
            print '***Warning*** : [ 0_ipa ]文件夹中没有ipa包'
        else:
            return count

    else:
        # os.mkdir(os.path.join(WDir, ipaPath))
        print '***Warning*** : 找不到[ 0_ipa ]文件夹'
        exit(0)

# 解压要处理的ipa包
def open_ipaFile():
    '解压要处理的ipa包'
    count = show_ipaList()
    # 输入选择的序号
    number = int(raw_input('请输入要操作的ipa包编号:'))
    if number <= 0 or number > count:
        open_ipaFile()
        return
    # 根据编号获取ipa包名字
    (status, result) = commands.getstatusoutput(
        '%s -c "Print :%s:%d" %s' % (PlistBuddy, ipaList, number - 1, os.path.join(WDir, packConfig)))
    if status:
        print '执行报错:%s' % result
    else:
        YN = raw_input('[ %s ]直接回车确认:' % result)
        if len(YN) == 0:
            # 获取路径
            path = os.path.join(os.path.join(WDir, ipaPath), '%s.ipa' % result)

            if os.path.isfile(path):
                # 缓存正在操作的ipa包名字
                (status, result) = commands.getstatusoutput(
                    '%s -c "Add ipaName string %s" %s' % (PlistBuddy, result, os.path.join(WDir, packConfig)))
                if status:
                    print '执行报错:%s' % result

                # 删除已存在的Payload文件夹
                if os.path.isdir(os.path.join(WDir, 'Payload')):
                    shutil.rmtree(os.path.join(WDir, 'Payload'))

                # 解压ipa到工作目录
                tool.unzip_file(path, WDir)
                # commands.getstatusoutput('unzip -d %s %s' % (WDir, tool.change_symbol(path)))
                # 删除 [ __MACOSX ]垃圾文件
                if os.path.isdir(os.path.join(WDir, '__MACOSX')):
                    shutil.rmtree(os.path.join(WDir, '__MACOSX'))
            else:
                print '***错误*** : 没有找到要操作的ipa包'
        else:
            open_ipaFile()


# 显示渠道SDK列表
def show_channleList():
    '显示渠道SDK列表'
    if os.path.isdir(os.path.join(WDir, channelPath)):
        # print '[ 0_Channel ]存在\n'
        #删除旧缓存，创建新缓存
        (status, result) = commands.getstatusoutput('%s -c "Delete :%s" %s' % (PlistBuddy, channelList, os.path.join(WDir, packConfig)))
        if status:
            # print '执行报错:%s' % result
            pass
        (status, result) = commands.getstatusoutput('%s -c "Add :%s array" %s' % (PlistBuddy, channelList, os.path.join(WDir, packConfig)))
        if status:
            print '执行报错:%s' % result

        # 遍历0_Channle文件夹中的文件列表
        print ' 渠道SDK列表 '.center(80, '*')
        fileList = os.listdir(os.path.join(WDir, channelPath))
        count = 0
        for i in range(len(fileList)):
            if fileList[i].endswith('.zip'):
                path = os.path.join(os.path.join(WDir, channelPath), fileList[i])
                if os.path.isfile(path):
                    channelName = fileList[i].replace('.zip', '')
                    print '[%d] - %s' % (count + 1, channelName)
                    (status, result) = commands.getstatusoutput('%s -c "Add :%s:%d string %s" %s' % (PlistBuddy, channelList, count, channelName, os.path.join(WDir, packConfig)))
                    if status:
                        print '执行报错:%s' % result
                    else:
                        count += 1
            else:
                pass
                # print '[-] - (无效文件)%s' % fileList[i]

        print ' 渠道SDK列表 '.center(80, '*')
        if count == 0:
            print '***Warning*** : [ 0_Channel ]文件夹中没有渠道SDK包'
        else:
            return count
    else:
        # os.mkdir(os.path.join(WDir, channelPath))
        print '***Warning*** : 找不到[ 0_Channel ]文件夹'
        exit(0)


# 解压要使用的渠道SDK包
def open_channelFile():
    '解压要使用的渠道SDK包'
    count = show_channleList()
    # 输入要操作的渠道序号
    number = int(raw_input('请输入要集成的SDK渠道商编号:'))
    if number <= 0 or number > count:
        open_channelFile()
        return
    # 根据编号获取渠道商名
    (status, result) = commands.getstatusoutput('%s -c "Print :%s:%d" %s' % (PlistBuddy, channelList, number - 1, os.path.join(WDir, packConfig)))
    if status:
        print '执行报错:%s' % result
    else:
        YN = raw_input('[ %s ]直接回车确认:' % result)
        if len(YN) == 0:
            # 获取路径
            path = os.path.join(os.path.join(WDir, channelPath), '%s.zip' % result)
            if os.path.isfile(path):
                # 渠道商名
                channelName = result
                # 缓存正在使用的渠道商名
                (status, result) = commands.getstatusoutput('%s -c "Add channelName string %s" %s' % (PlistBuddy, channelName, os.path.join(WDir, packConfig)))
                if status:
                    print '执行报错:%s' % result

                # 删除已存在的渠道商文件夹
                if os.path.isdir(os.path.join(WDir, channelName)):
                    shutil.rmtree(os.path.join(WDir, channelName))

                # 解压到工作目录
                tool.unzip_file(path, WDir)
                # 删除 [ __MACOSX ]垃圾文件
                if os.path.isdir(os.path.join(WDir, '__MACOSX')):
                    shutil.rmtree(os.path.join(WDir, '__MACOSX'))
            else:
                print '***错误*** : 没有找到要集成的渠道SDK'
        else:
            open_channelFile()


# 集成SDK资源文件到ipa包
def importSDK():
    '''集成SDK资源文件到ipa包'''
    open_ipaFile()
    open_channelFile()
    # 获取.app的名字
    global appName  # 声明全局变量
    appNameList = os.listdir(os.path.join(WDir, 'Payload'))
    for i in range(len(appNameList)):
        if appNameList[i].endswith('.app'):
            appName = appNameList[i]
        else:
            path = os.path.join(os.path.join(WDir, 'Payload'), appNameList[i])
            os.remove(path)

    # 获取ipa包Info.plist路径
    global ipaPlist
    ipaPlist = '%s/Payload/%s/Info.plist' % (WDir, appName)

    # 判断app包里面是否存在Frameworks文件夹，不存在则创建
    if os.path.isdir('%s/Payload/%s/Frameworks' % (WDir, appName)):
        pass
    else:
        os.mkdir('%s/Payload/%s/Frameworks' % (WDir, appName))

    #获取渠道文件名
    (status, result) = commands.getstatusoutput('%s -c "Print :channelName" %s' % (PlistBuddy, os.path.join(WDir, packConfig)))
    if status:
        print '执行报错:%s' % result
    else:
        channelName = result
        # 移动Frameworks文件中的文件
        tool.move_files('%s/Frameworks' % os.path.join(WDir, channelName), '%s/Payload/%s/Frameworks' % (WDir, appName), '-n')

        # 移动渠道文件夹中的文件
        tool.move_files('%s' % os.path.join(WDir, channelName), '%s/Payload/%s' % (WDir, appName), '-n')


# 合并plist信息
def merge_plist():
    '合并plist信息'
    print ' 合并plist信息 '.center(80, '*')
    # print ipaPlist
    if os.path.isfile(os.path.join(WDir, packConfig)):
        # 获取渠道文件名
        (status, result) = commands.getstatusoutput('%s -c "Print :channelName" %s' % (PlistBuddy, os.path.join(WDir, packConfig)))
        if status:
            print '执行报错:%s' % result
        else:
            channelName = result
            # 判断渠道文件夹下Info.plist是否存在
            if os.path.isfile('%s/Info.plist' % os.path.join(WDir, channelName)):

                print '(a).合并权限尺寸信息'
                (status, result) = commands.getstatusoutput('%s -c "Merge %s/Info.plist" %s' % (PlistBuddy, tool.change_symbol(os.path.join(WDir, channelName)), tool.change_symbol(ipaPlist)))
                if status:
                    print '执行报错:%s(%s)' % (result, '%s -c "Merge %s/Info.plist" %s' % (PlistBuddy, tool.change_symbol(os.path.join(WDir, channelName)), tool.change_symbol(ipaPlist)))
                    exit()

                print '(b).合并白名单'
                (status, result) = commands.getstatusoutput('%s -c "Print :LSApplicationQueriesSchemes" %s/Info.plist' % (PlistBuddy, tool.change_symbol(os.path.join(WDir, channelName))))
                if status:
                    print '执行报错:%s' % result
                else:
                    result = result.replace('Array', '')
                    result = result.replace(' ', '')
                    result = result.replace('{', '')
                    result = result.replace('}', '')
                    listArr = result.split('\n')
                    for i in range(0, len(listArr)):
                        if len(listArr[i]) > 0:
                            # print listArr[i]
                            (status, result) = commands.getstatusoutput('%s -c "Add :LSApplicationQueriesSchemes:%d string %s" %s' % (PlistBuddy, i, listArr[i], tool.change_symbol(ipaPlist)))
                            if status:
                                print '执行报错:%s' % result

                print '(c).配置URL Types'
                for i in range(0, 99):
                    (status, result) = commands.getstatusoutput('%s -c "Print :CFBundleURLTypes:%d" %s/Info.plist' % (PlistBuddy, i, tool.change_symbol(os.path.join(WDir, channelName))))
                    if status:
                        # print '执行报错:%s' % result
                        break
                    else:
                        # 读取信息
                        CFBundleTypeRole = ''
                        CFBundleURLName = ''
                        CFBundleURLSchemes = ''

                        # CFBundleTypeRole
                        (status, result) = commands.getstatusoutput('%s -c "Print :CFBundleURLTypes:%d:CFBundleTypeRole" %s/Info.plist' % (PlistBuddy, i, tool.change_symbol(os.path.join(WDir, channelName))))
                        if status:
                            # print '执行报错:%s' % result
                            pass
                        else:
                            CFBundleTypeRole = result

                        # CFBundleURLName
                        (status, result) = commands.getstatusoutput('%s -c "Print :CFBundleURLTypes:%d:CFBundleURLName" %s/Info.plist' % (PlistBuddy, i, tool.change_symbol(os.path.join(WDir, channelName))))
                        if status:
                            # print '执行报错:%s' % result
                            pass
                        else:
                            CFBundleURLName = result

                        # CFBundleURLSchemes
                        (status, result) = commands.getstatusoutput('%s -c "Print :CFBundleURLTypes:%d:CFBundleURLSchemes" %s/Info.plist' % (PlistBuddy, i, tool.change_symbol(os.path.join(WDir, channelName))))
                        if status:
                            # print '执行报错:%s' % result
                            pass
                        else:
                            CFBundleURLSchemes = result

                        # print CFBundleTypeRole, CFBundleURLName, CFBundleURLSchemes

                        # 填入信息
                        (status, result) = commands.getstatusoutput('%s -c "Add :CFBundleURLTypes:0 dict" %s' % (PlistBuddy, tool.change_symbol(ipaPlist)))
                        if status:
                            print '执行报错:%s' % result
                        else:
                            if len(CFBundleTypeRole) > 0:
                                (status, result) = commands.getstatusoutput('%s -c "Add :CFBundleURLTypes:0:CFBundleTypeRole string %s" %s' % (PlistBuddy, CFBundleTypeRole, tool.change_symbol(ipaPlist)))
                                if status:
                                    print '执行报错:%s' % result

                            if len(CFBundleURLName) > 0:
                                (status, result) = commands.getstatusoutput('%s -c "Add :CFBundleURLTypes:0:CFBundleURLName string %s" %s' % (PlistBuddy, CFBundleURLName, tool.change_symbol(ipaPlist)))
                                if status:
                                    print '执行报错:%s' % result

                            if len(CFBundleURLSchemes) > 0:
                                (status, result) = commands.getstatusoutput('%s -c "Add :CFBundleURLTypes:0:CFBundleURLSchemes array" %s' % (PlistBuddy, tool.change_symbol(ipaPlist)))
                                if status:
                                    print '执行报错:%s' % result
                                else:
                                    j = 0
                                    while(j < 99):
                                        (status, result) = commands.getstatusoutput('%s -c "Print :CFBundleURLTypes:%d:CFBundleURLSchemes:%d" %s/Info.plist' % (PlistBuddy, i, j, tool.change_symbol(os.path.join(WDir, channelName))))
                                        if status:
                                            # print '执行报错:%s' % result
                                            break
                                        else:
                                            input_URLSchemes = raw_input('按提示输入[URL Schemes](提示:%s):' % result)
                                            if len(input_URLSchemes) <= 0:
                                                j = j - 1
                                            else:
                                                (status, result) = commands.getstatusoutput('%s -c "Add :CFBundleURLTypes:0:CFBundleURLSchemes:0 string %s" %s' % (PlistBuddy, input_URLSchemes, tool.change_symbol(ipaPlist)))
                                                if status:
                                                    print '执行报错:%s' % result
                                        j = j + 1
            else:
                print '没有渠道Info.plist文件'


# 配置plist参数
def setInfoPlist():
    '配置plist表的参数'
    print '配置plist表的参数'.center(80, '*')
    # 应用名
    (status, result) = commands.getstatusoutput('%s -c "Print :CFBundleDisplayName" %s' % (PlistBuddy, tool.change_symbol(ipaPlist)))
    if status:
        print '执行报错:%s' % result
    else:
        displayName = raw_input('应用名称(默认:%s):' % result)
        if len(displayName) <= 0:
            displayName = result
        (status, result) = commands.getstatusoutput('%s -c "Set :CFBundleDisplayName %s" %s' % (PlistBuddy, displayName, tool.change_symbol(ipaPlist)))
        if status:
            print '执行报错:%s' % result
        (status, result) = commands.getstatusoutput('%s -c "Set :CFBundleName %s" %s' % (PlistBuddy, displayName, tool.change_symbol(ipaPlist)))
        if status:
            print '执行报错:%s' % result

    # bundleID
    (status, result) = commands.getstatusoutput('%s -c "Print :CFBundleIdentifier" %s' % (PlistBuddy, tool.change_symbol(ipaPlist)))
    if status:
        print '执行报错:%s' % result
    else:
        bundleID = raw_input('应用BundleID(默认:%s):' % result)
        if len(bundleID) <= 0:
            bundleID = result
        (status, result) = commands.getstatusoutput('%s -c "Set :CFBundleIdentifier %s" %s' % (PlistBuddy, bundleID, tool.change_symbol(ipaPlist)))
        if status:
            print '执行报错:%s' % result

    # 版本号
    (status, result) = commands.getstatusoutput('%s -c "Print :CFBundleShortVersionString" %s' % (PlistBuddy, tool.change_symbol(ipaPlist)))
    if status:
        print '执行报错:%s' % result
    else:
        version = raw_input('应用版本号(默认:%s):' % result)
        if len(version) <= 0:
            version = result
        (status, result) = commands.getstatusoutput('%s -c "Set :CFBundleShortVersionString %s" %s' % (PlistBuddy, version, tool.change_symbol(ipaPlist)))
        if status:
            print '执行报错:%s' % result

    #bundle
    (status, result) = commands.getstatusoutput('%s -c "Print :CFBundleVersion" %s' % (PlistBuddy, tool.change_symbol(ipaPlist)))
    if status:
        print '执行报错:%s' % result
    else:
        bundle = raw_input('Bundle(默认:%s):' % result)
        if len(bundle) <= 0:
            bundle = result
        (status, result) = commands.getstatusoutput('%s -c "Set :CFBundleVersion %s" %s' % (PlistBuddy, bundle, tool.change_symbol(ipaPlist)))
        if status:
            print '执行报错:%s' % result

    YN = raw_input("[ 应用信息 ]直接回车确认:")
    if len(YN) == 0:
        # 合并信息
        merge_plist()
    else:
        setInfoPlist()


# 设置SDK参数表
def setSDKConfig():
    '设置SDK参数表'
    print ' 设置SDK参数表 '.center(80, '*')
    (status, result) = commands.getstatusoutput('%s -c "Print :channelName" %s' % (PlistBuddy, os.path.join(WDir, packConfig)))
    if status:
        print '执行报错:%s' % result
    else:
        channelName = result
        # 获取参数表信息
        (status, result) = commands.getstatusoutput('%s -c "Print :plistConfigPath" %s/Info.plist' % (PlistBuddy, tool.change_symbol(os.path.join(WDir, channelName))))
        if status:
            print '执行报错:%s' % result
        else:
            result = result.replace('Array', '')
            result = result.replace(' ', '')
            result = result.replace('{', '')
            result = result.replace('}', '')
            listArr = result.split('\n')

            for i in range(0, len(listArr)):
                if len(listArr[i]) > 0:
                    # 移出参数表
                    tool.move_files('%s/Payload/%s/%s' % (WDir, appName, listArr[i]), os.path.join(WDir, channelName))

            YN = raw_input('确认已配置好参数表(Y/N):')
            if YN == 'Y' or YN == 'y':
                # 缓存服务器和渠道号
                (status, result) = commands.getstatusoutput('%s -c "Print :server_config:server_type" %s' % (PlistBuddy, '%s/app_config.plist' % tool.change_symbol(os.path.join(WDir, channelName))))
                if status:
                    print '执行报错:%s' % result
                else:
                    (status, result) = commands.getstatusoutput('%s -c "Add server_type string %s" %s' % (PlistBuddy, result, os.path.join(WDir, packConfig)))
                    if status:
                        print '执行报错:%s' % result

                (status, result) = commands.getstatusoutput('%s -c "Print :server_config:server_channel" %s' % (PlistBuddy, '%s/app_config.plist' % tool.change_symbol(os.path.join(WDir, channelName))))
                if status:
                    print '执行报错:%s' % result
                else:
                    (status, result) = commands.getstatusoutput('%s -c "Add server_channel string %s" %s' % (PlistBuddy, result, os.path.join(WDir, packConfig)))
                    if status:
                        print '执行报错:%s' % result

                for i in range(0, len(listArr)):
                    if len(listArr[i]) > 0:
                        # 移回参数表
                        tool.move_files('%s/%s' % (os.path.join(WDir, channelName), listArr[i]), '%s/Payload/%s' % (WDir, appName))
            else:
                exit()


# 导入图片
def importAssets():
    '导入icon和LaunchImage'
    # 创建一个新的路劲
    if os.path.isdir(os.path.join(WDir, assets)):
        shutil.rmtree(os.path.join(WDir, assets))
    os.mkdir(os.path.join(WDir, assets))

    # 获取bundleID,处理图片需要
    (status, result) = commands.getstatusoutput('%s -c "Print :CFBundleIdentifier" %s' % (PlistBuddy, tool.change_symbol(ipaPlist)))
    if status:
        print '执行报错:%s' % result
    else:
        bundleID = result
        YN = raw_input('确认已将图片资源放到指定文件夹(Assets)(Y/N):')
        if YN == 'Y' or YN == 'y':
            os.mkdir('%s/out_Assets' % os.path.join(WDir, assets))
            if os.path.isdir('%s/Assets.xcassets' % os.path.join(WDir, assets)):
                (status, result) = commands.getstatusoutput('/Applications/Xcode.app/Contents/Developer/usr/bin/actool --output-format human-readable-text --notices --warnings --export-dependency-info %s/assetcatalog_dependencies --output-partial-info-plist %s/assetcatalog_generated_info.plist --app-icon AppIcon --launch-image LaunchImage --compress-pngs --enable-on-demand-resources YES --sticker-pack-identifier-prefix %s.sticker-pack. --target-device iphone --target-device ipad --minimum-deployment-target 8.0 --platform iphoneos --product-type com.apple.product-type.application --compile %s/out_Assets %s/Assets.xcassets' % (os.path.join(WDir, assets), os.path.join(WDir, assets), bundleID, os.path.join(WDir, assets), os.path.join(WDir, assets)))
                if status:
                    print '执行报错:%s' % result
                else:
                    # 替换移动已处理好的图片到ipa
                    tool.move_files('%s/out_Assets' % os.path.join(WDir, assets), '%s/Payload/%s' % (WDir, appName))
            else:
                print '***warning*** : 需要放入图片资源'
                exit()
        else:
            print '***warning*** : 需要放入图片资源'
            exit()



# ipa包签名
def resign_ipa():
    'ipa包签名'
    global newName, WDir
    # 获取渠道文件名
    channelName = ''
    (status, result) = commands.getstatusoutput('%s -c "Print :channelName" %s' % (PlistBuddy, os.path.join(WDir, packConfig)))
    if status:
        print '执行报错:%s' % result
    else:
        channelName = result


    newName = ipaReSign.resign(WDir, channelName)


# 获取支付回调
def get_payCallback(channelName = '', serverType = '', serverChannel = ''):
    '支付回调'
    # 渠道名
    if len(channelName) == 0:
        (status, output) = commands.getstatusoutput('%s -c "Print :channelName" %s' % (PlistBuddy, os.path.join(WDir, packConfig)))
        if status:
            print '%s:%s' % ('%s -c "Print :channelName" %s' % (PlistBuddy, os.path.join(WDir, packConfig)), output)
        else:
            channelName = output

    # 服务器
    if len(serverType) == 0:
        (status, output) = commands.getstatusoutput('%s -c "Print :server_type" %s' % (PlistBuddy, os.path.join(WDir, packConfig)))
        if status:
            print '%s:%s' % ('%s -c "Print : server_type" %s' % (PlistBuddy, os.path.join(WDir, packConfig)), output)
        else:
            serverType = output

    # 渠道号
    if len(serverChannel) == 0:
        (status, output) = commands.getstatusoutput('%s -c "Print :server_channel" %s' % (PlistBuddy, os.path.join(WDir, packConfig)))
        if status:
            print '%s:%s' % ('%s -c "Print :server_channel" %s' % (PlistBuddy, os.path.join(WDir, packConfig)), output)
        else:
            serverChannel = output

    # 获取服务器URL
    serverURL = ''
    (status, output) = commands.getstatusoutput('%s -c "Print :ServerURL:%s" %s/payCallbackConfig.plist' % (PlistBuddy, serverType, os.path.join(WDir, channelPath)))
    if status:
        # print '%s:%s' % ('%s -c "Print :ServerURL:%s" %s/payCallbackConfig.plist' % (PlistBuddy, serverType, os.path.join(WDir, channelPath)), output)
        serverURL = serverType
    else:
        serverURL = output

    # 获取渠道脚本路径
    channelURL = ''
    (status, output) = commands.getstatusoutput('%s -c "Print :ChannelURL:%s" %s/payCallbackConfig.plist' % (PlistBuddy, channelName, os.path.join(WDir, channelPath)))
    if status:
        # print '%s:%s' % ('%s -c "Print :ChannelURL:%s" %s/payCallbackConfig.plist' % (PlistBuddy, channelName, os.path.join(WDir, channelPath)), output)
        channelURL = channelName
    else:
        channelURL = output

    payCallback = '%s/pay/%s/payCallback/%s' % (serverURL, channelURL, serverChannel)
    print ' 支付回调地址 '.center(80, '*')
    if len(newName): print newName + ':'
    print payCallback
    print ' 支付回调地址 '.center(80, '*')






# 清理不需要的垃圾文件
def cleanJunkFiles():
    # 遍历清理ipa文件夹不需要的文件
    fileList = os.listdir('%s/' % os.path.join(WDir, ipaPath))
    for i in range(0, len(fileList)):
        if fileList[i].endswith('.ipa'):
            pass
        else:
            # 获取路径
            path = os.path.join(os.path.join(WDir, ipaPath), fileList[i])
            if os.path.isfile(path):
                os.remove(path)
            if os.path.isdir(path):
                shutil.rmtree(path)

    # 遍历清理channel文件夹
    fileList = os.listdir(os.path.join(WDir, channelPath))
    for i in range(0, len(fileList)):
        if fileList[i].endswith('.zip') or fileList[i].endswith('.plist'):
            pass
        else:
            path = os.path.join(os.path.join(WDir, channelPath), fileList[i])
            if os.path.isfile(path):
                os.remove(path)
            if os.path.isdir(path):
                shutil.rmtree(path)


    # 删除临时文件夹
    YN = raw_input('是否清除打包生成的垃圾文件(Y/N):')
    if YN == 'Y' or YN == 'y':
        # 删除渠道商文件夹
        (status, result) = commands.getstatusoutput('%s -c "Print :channelName" %s' % (PlistBuddy, os.path.join(WDir, packConfig)))
        if status:
            pass
        else:
            if os.path.isdir(os.path.join(WDir, result)):
                shutil.rmtree(os.path.join(WDir, result))

        # 删除Payload
        if os.path.isdir(os.path.join(WDir, 'Payload')):
            shutil.rmtree(os.path.join(WDir, 'Payload'))

        # 删除图片资源文件夹
        if os.path.isdir(os.path.join(WDir, assets)):
            shutil.rmtree(os.path.join(WDir, assets))

        # 删除签名信息文件
        if os.path.isfile(os.path.join(WDir, 'entitlements.plist')):
            os.remove(os.path.join(WDir, 'entitlements.plist'))
        if os.path.isfile(os.path.join(WDir, 'mobileprovision.plist')):
            os.remove(os.path.join(WDir, 'mobileprovision.plist'))

        # 删除打包信息缓存文件
        if os.path.isfile(os.path.join(WDir, packConfig)):
            os.remove(os.path.join(WDir, packConfig))

        print '垃圾文件清理完成'